package se.vgregion.icc.handlers;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import se.riv.itintegration.dependencies.v1.AbstractDependency;
import se.riv.itintegration.dependencies.v1.IntegrationComponentDependency;
import se.riv.itintegration.monitoring.pingforconfigurationresponder.v1.ObjectFactory;
import se.riv.itintegration.monitoring.pingforconfigurationresponder.v1.PingForConfigurationResponseType;
import se.riv.itintegration.monitoring.pingforconfigurationresponder.v1.PingForConfigurationType;
import se.vgregion.icc.handlers.annotations.PingDependencyHandler;
import se.vgregion.icc.riv.itintegration.monitoring.PingForConfigurationConsumer;

/***
 * Handles pinging of integration components.
 * 
 * @author ricra2 & andan40
 * @see {@link se.vgregion.icc.handlers.PingHandler}
 * @see {@link se.riv.itintegration.monitoring.pingforconfiguration.v1.rivtabp21.PingForConfigurationResponderInterface}
 */
@PingDependencyHandler
public class ComponentPingHandler implements PingHandler {
    private static final Logger LOG = LoggerFactory.getLogger(ComponentPingHandler.class);

    /***
     * {@inheritDoc}
     */
    @Override
    public String preformPingAndReturnResponse(AbstractDependency dependency, PingForConfigurationType parameters) {

        PingForConfigurationResponseType responseType =
            new PingForConfigurationConsumer(dependency.getUrl()).getResponse(parameters);

        return marshal(responseType);
    }

    /***
     * {@inheritDoc}
     * @return IntegrationComponentDependency.class
     */
    @SuppressWarnings("rawtypes")
    @Override
    public Class getHandledDependencyClass() {
        return IntegrationComponentDependency.class;
    }

    /***
     * {@inheritDoc}
     */
    @Override
    public String marshal(Object responseType) {
        String marshalledRespose = "";

        JAXBElement<PingForConfigurationResponseType> jaxbPingForConfigurationResponse = null;
        if (responseType instanceof PingForConfigurationResponseType) {
            PingForConfigurationResponseType pingForConfigurationResponseType =
                (PingForConfigurationResponseType) responseType;
            ObjectFactory objectFactory = new ObjectFactory();
            jaxbPingForConfigurationResponse =
                objectFactory.createPingForConfigurationResponse(pingForConfigurationResponseType);
        }

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(PingForConfigurationResponseType.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            StringWriter stringWriter = new StringWriter();

            jaxbMarshaller.marshal(jaxbPingForConfigurationResponse, stringWriter);

            marshalledRespose = stringWriter.toString();

        } catch (Exception e) {
            LOG.error("Couldn't marshal the object " + responseType);
        }
        return marshalledRespose;
    }
}
